@extends('layouts.header')
@section('title','检测设备套装组合')
@section('content')
    <nav class="breadcrumb">
        <a class="btn btn-primary radius" onclick="Export()">当前页导出xls</a>
        <a class="btn btn-primary radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.reload();" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a>
    </nav>
    <div class="page-container">
        <div class="cl bg-1 bk-gray">
            <form class="form form-horizontal" id="myform">
                <div class="row cl">
                    <label class="form-label col-sm-2">组合名称：</label>
                    <div class="formControls col-sm-6">
                        <input type="text" class="input-text required" name="name" maxlength="50" onkeyup="checkName(this)" />
                    </div>
                </div>
                <div class="row cl">
                    <label class="form-label col-sm-2">检测设备：</label>
                    <div class="formControls col-sm-8">
                        <select class="select2 required" name="station_ids[]" id="station_ids" style="width:100%;" multiple>
                            @foreach($data['stations'] as $station)
                                <option value="{{$station->id}}">{{$station->name}} -- {{$station->dev_num}}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="row cl text-c">
                    <b class="btn btn-primary radius" onclick="edit()">添加/修改</b>
                    <b class="btn btn-danger radius ml-15" onclick="del()">删除</b>
                </div>
            </form>
        </div>
        <div class="mt-10">
            <b class="c-red mb-5">以下表格内容点击后在上面编辑</b>
            <table class="table table-border table-bordered table-hover table-bg" id="datatable">
                <thead>
                    <tr class="text-c">
                        <th>组合名称</th>
                        <th>仪器设备名称</th>
                        <th>设备编号</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($data['list'] as $name => $item)
                    <tr class="text-l" data-station_ids="{{implode('，',array_column($item,'station_id'))}}">
                        <td class="name">{{$name}}</td>
                        <td class="station_name">{{implode('，',array_column($item,'station_name'))}}</td>
                        <td class="dev_num">{{implode('，',array_column($item,'dev_num'))}}</td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
    <script type="text/javascript" src="/admin/static/myfolder/js/jquerytable2excel.js"></script>
    <script type="text/javascript" src="/admin/static/myfolder/js/printBarCode.js?ver={{config('constant.static_res_ver')}}"></script>

    <script type="text/javascript">
        @php 
            $setting_info = DB::table('setting_sys')->where('key', 'print_bar_code')->value('value'); //设置信息
            $setting_info = json_decode($setting_info, true);
        @endphp
        var is_remote_print = {{ isset($setting_info['is_remote']) ? $setting_info['is_remote'] : 0}},//为本机打印或远程打印(本机打印:0 , 远程打印:1)
            remote_ip = '127.0.0.1';
        $(function () {
            $.fn.dataTable.ext.errMode = 'throw'; //datatable抛异常的方式，在console中输出，默认以alert的方式弹出
            $('#datatable').DataTable({
                // order:[[3,'asc']]
                scrollX:true
            });
        });

        function checkName(obj) {
            let name = $(obj).val();
            $('tbody td.name','#datatable').each(function() {
                if ($(this).text() == name) {
                    $(this).closest('tr').trigger('click');
                    return false;
                }
                $('#station_ids').select2().val('').trigger('change');
            })
        }

        $('tbody tr','#datatable').on('click',function(){
            let name = $(this).find('.name').text(),
                station_ids = $(this).data('station_ids').toString().split('，');
            $('input[name=name]').val(name);
            $('#station_ids').select2().val(station_ids).trigger('change');
        })

        function Export() {
            $("#datatable").table2excel({            //exceltable为存放数据的table
                // 不被导出的表格行的CSS class类
                exclude: ".noExl",
                // 导出的Excel文档的名称
                name: "设备管理台账",
                // Excel文件的名称
                filename: "设备管理台账",
                bootstrap: true
            });
        }

        /*表格重新加载*/
        function table_reload(bool) {
            //清除表头 CheckBox的对勾
            $("#datatable").find("thead").find("input[type=checkbox]").removeAttr('checked');
            location.reload();
        }

        //添加、编辑
        function edit() {
            let form = $('#myform');
            if (!form.valid()) {
                layer.msg('请先完善表单！');
                return false;
            }
            ajax('post','edit',form.serialize(),editBak);
            function editBak(res) {
                if(res == 1){
                    location.reload();
                }
            }
        }

        /*删除*/
        function del(id, obj) {
            let form = $('#myform');
            if (!form.valid()) {
                layer.msg('请先完善表单！');
                return false;
            }
            
            layer.confirm('确认要删除吗？', function (index) {
                ajax('post','del',form.serialize(),delBak);
                function delBak(res) {
                    if(res == 1){
                        location.reload();
                    }
                }
            });
        }

    </script>
@endsection